package com.github.shiqiyue.app.api.sapi.controller.dbms;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.shiqiyue.app.api.sapi.common.SApiBaseController;
import com.github.shiqiyue.app.api.sapi.controller.dbms.dto.DatabaseHistoryPageDTO;
import com.github.shiqiyue.app.api.sapi.controller.dbms.req.DatabaseHistoryPageReq;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.modules.dbms.entity.DatabaseHistory;
import com.github.shiqiyue.app.modules.dbms.service.IDatabaseHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

/**
 * <p>
 * 数据库-历史记录 前端控制器
 * </p>
 *
 * @author wenyao.wu
 * @since 2019-08-06
 */
@RestController
@RequestMapping("/sapi/dbms/database-history")
public class ApiDatabaseHistoryController extends SApiBaseController {

    @Autowired
    private IDatabaseHistoryService databaseHistoryService;

    /***
     * 分页
     * @param databaseHistoryPageReq
     * @return
     */
    @PostMapping("page")
    public Result page(DatabaseHistoryPageReq databaseHistoryPageReq) {
        IPage<DatabaseHistory> pageResult = databaseHistoryService.page(databaseHistoryPageReq);
        DatabaseHistoryPageDTO databaseHistoryDTO = new DatabaseHistoryPageDTO();
        databaseHistoryDTO.setCurrent(pageResult.getCurrent());
        databaseHistoryDTO.setSize(pageResult.getSize());
        databaseHistoryDTO.setTotal(pageResult.getTotal());
        databaseHistoryDTO.setRecords(pageResult.getRecords().stream().map(DatabaseHistoryPageDTO.Item::newInstance).collect(Collectors.toList()));
        return Result.success(databaseHistoryDTO);
    }

}
